package com.nanhang.controller;

import com.nanhang.common.util.PageResult;
import com.nanhang.common.util.R;
import com.nanhang.entity.employee;
import com.nanhang.vo.employeeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.nanhang.service.employeeService;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/employee")
public class employeeController {
    @Autowired
    private employeeService employeeService;
    @GetMapping("/list")
    public R findAll(){
        List<employee> list = employeeService.list();
        return R.ok().data("list",list);
    }
    // 分页查询
    @GetMapping("/findByPage/{page}/{pageSize}")
    public R findByPage(@PathVariable("page") Integer page,
                        @PathVariable("pageSize") Integer pageSize) {
        PageResult<employee> pr = employeeService.findByPage(page,pageSize);
        return R.ok().data("pr",pr);
    }
    //条件分页查询
    @PostMapping("/search/{page}/{pageSize}")
    public R search(@PathVariable("page") Integer page,
                    @PathVariable("pageSize") Integer pageSize,
                    @RequestBody(required = false) employeeVO vo){
        PageResult<employee> pr = employeeService.search(page,pageSize,vo);
        return R.ok().data("pr",pr);
    }
    //删除
    @DeleteMapping("/delete/{ids}")
    public R delete(@PathVariable("ids") String ids){  // 1,2,3
        String[] split = ids.split(",");
        List<String> eids = Arrays.asList(split);
        return employeeService.removeByIds(eids) ? R.ok().message("删除员工成功!") : R.error().message("删除员工失败!"+eids);
    }
    //修改 修改员工信息只有利用自带函数即可
    @PutMapping("/update")
    public R update(@RequestBody employee employee){
        boolean flag = employeeService.updateById(employee);
        return flag ? R.ok().message("修改员工成功!") : R.error().message("修改员工失败!");
    }
    //添加
    @PostMapping("/add")
    public R add(@RequestBody employee employee){
        boolean flag = employeeService.add(employee);
        return flag ? R.ok().message("添加员工成功!") : R.error().message("添加员工失败!");
    }
}

